<template>
  <page-header-wrapper :title="false">
    <a-card title="账户详情">
      <a-descriptions bordered title="" size="small">
        <a-descriptions-item label="订单" :span="3"> {{ info?.orderId }} </a-descriptions-item>
        <a-descriptions-item label="供应商" :span="3"> {{ info?.supplyName }} </a-descriptions-item>
        <a-descriptions-item label="账户类型" :span="3">
          <template>
            <a-tag color="#D15B47" v-if="info?.spAccountTypes == '0'">待上账</a-tag>
            <a-tag color="#82af6f" v-if="info?.spAccountTypes == '1'">已上账</a-tag>
          </template>
        </a-descriptions-item>
        <a-descriptions-item label="类型" :span="3"> {{ render(info?.types) }} </a-descriptions-item>
        <a-descriptions-item label="创建人" :span="3"> {{ info?.empName }} </a-descriptions-item>
        <a-descriptions-item label="创建日期" :span="3"> {{ info?.date }} </a-descriptions-item>
        <a-descriptions-item label="金额" :span="3"> {{ info?.amount }} 元 </a-descriptions-item>
        <a-descriptions-item label="账户" :span="3"><a @click="handleZh(info)"> {{ info?.recordAccName }} </a></a-descriptions-item>
        <a-descriptions-item label="审核状态" :span="3">
          <template>
            <a-tag color="#82af6f" v-if="info?.checkStatus == '1'">已审核</a-tag>
            <a-tag color="#D15B47" v-if="info?.checkStatus == '0'">未审核</a-tag>
          </template>
        </a-descriptions-item>
        <a-descriptions-item label="备注" :span="3"> {{ info?.memo }} </a-descriptions-item>
        <a-descriptions-item label="操作" :span="3">
          <div style="display: flex; margin-top: 10px">
            <a-button type="danger" style="margin: 0px 5px" v-if="info?.checkStatus == '0'" @click="setCX()">撤销订单</a-button>
            <a-button type="primary" style="margin-right: 5px" @click="handleEdit(info)" v-if="info?.checkStatus == '0'">编辑订单</a-button
            >
            <a-button type="primary" style="margin: 0px 5px" v-if="info?.checkStatus == '0'" @click="setSH()">审核订单</a-button>
            <a-button style="margin: 0px 5px" v-if="info?.checkStatus == '1'" @click="setFSH()">反审核</a-button>
          </div>
        </a-descriptions-item>
      </a-descriptions>
    </a-card>
    <a-card title="操作记录" style="margin-top: 24px">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="8">
              <a-form-item label="筛选">
                <a-input v-model="queryParam.keyword" placeholder="请输入" />
              </a-form-item>
            </a-col>
            <a-col :md="(!advanced && 8) || 24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" @click="handleSearch(queryParam)" :loading="recordLoading">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
                <!-- <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a> -->
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 
      <div class="table-operator">
        <a-button icon="cloud-upload">导出</a-button>
      </div> -->

      <a-table ref="table" size="default" rowKey="key" :columns="columns" :data-source="loadData">
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="option" slot-scope="text, record">
          <template> {{ record.empName }} <a-tag color="#428bca" @click="handleUserDetail(record)">详情</a-tag> </template>
        </span>
      </a-table>
    </a-card>
    <account-form
      ref="createModal"
      :visible="visible"
      :loading="confirmLoading"
      :model="mdll"
      :supplyTypes="this.$route.query.types"
      @cancel="handleCancel"
      @ok="handleOk"
    />
  </page-header-wrapper>
</template>

<script>
import { getOperRecordListUsingPOST } from '@/api/api/caigouguanli.ts'
import { getSuppliesAcclogUsingGET ,checkSuppliesAcclogUsingPOST,unCheckSuppliesAcclogUsingPOST,deleteSuppliesAcclogUsingPOST,updateSuppliesAcclogUsingPOST} from '@/api/api/jingxiaoshangyunyingshangguanli.ts'

import AccountForm from './AccountForm'
export default {
  name: 'AgentOrderDetail',
  components: {
    AccountForm,
  },
  data() {
    return {
      queryParam: {
        pageNo: 1,
        pageSize: 10,
        queryCondition: '',
        status: '',
      },
      info: {},
      advanced: false,
      mdll: null,
      recordLoading: false,
      mdl: null,
      visible: false,
      groupVisible: false,
      groupLoading: false,
      value: '',
      confirmLoading: false,
      columns: [
        {
          title: '时间',
          dataIndex: 'operTime',
        },
        {
          title: '类型',
          dataIndex: 'action',
        },
        {
          title: '操作',
          dataIndex: 'option',
          width: '150px',
          scopedSlots: { customRender: 'option' },
        },
      ],
      loadData: [],
    }
  },
  mounted() {
    this.handleSearch()
    this.getDetail()
  },
  methods: {
    handleUserDetail(record) {
      this.$router.push({ path: `/user/user/${record.operator}` })
    },
    setCX() {
      const that = this
      that.$confirm({
        title: '撤销订单',
        content: '你确定要撤销该订单吗？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          deleteSuppliesAcclogUsingPOST({ id: that.info.id })
            .then(() => {
              that.$router.back()
              that.handleSearch(that.queryParam)
              that.$message.success('操作成功')
            })
            .catch((error) => {
              that.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
        },
      })
    },
    setSH() {
      const that = this
      that.$confirm({
        title: '审核订单',
        content: '你确定要审核通过该订单吗？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          checkSuppliesAcclogUsingPOST({ id: that.info.id })
            .then(() => {
              that.getDetail()
              that.handleSearch(that.queryParam)
              that.$message.success('操作成功')
            })
            .catch((error) => {
              that.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
        },
      })
    },
    setFSH() {
      const that = this
      that.$confirm({
        title: '反审核',
        content: '你确定要反审核该订单吗？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          unCheckSuppliesAcclogUsingPOST({ id: that.info.id })
            .then(() => {
              that.getDetail()
              that.handleSearch(that.queryParam)
              that.$message.success('操作成功')
            })
            .catch((error) => {
              that.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
        },
      })
    },
    render(type) {
      switch (type) {
        case '1':
          return '采购入库'
        case '2':
          return '采购退库'
        case '3':
          return '保价'
        case '4':
          return '供应商回款'
        case '5':
          return '供应商退款'
        case '6':
          return '经销商收款'
        case '7':
          return '经销商退款'
        case '8':
          return '应收款'
        case '9':
          return '应付款'
      }
    },
    getDetail() {
      getSuppliesAcclogUsingGET({ orderId: this.$route.params.id })
        .then((res) => {
          this.info = res.data
        })
        .catch(() => {
          this.$message.error('获取详情失败,请联系管理员')
        })
    },
    handleSearch(params) {
      this.recordLoading = true
      getOperRecordListUsingPOST({ ...params, orderId: this.$route.params.id })
        .then((res) => {
          this.loadData = res.data.list
          this.recordLoading = false
          this.recordPagination = { ...this.recordPagination, total: Number(res.data.total) }
        })
        .catch((error) => {
          this.recordLoading = false
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    handleEdit(info) {
      this.mdll = { ...info }
      this.visible = true
    },
    handleOk() {
      this.confirmLoading = true
      const form = this.$refs.createModal.form
      form.validateFields((errors, values) => {
        if (!errors) {
          updateSuppliesAcclogUsingPOST({ ...values })
            .then(() => {
              this.getDetail()
              this.handleSearch(this.queryParam)
              this.visible = false
              this.confirmLoading = false
              this.$message.success('修改成功')
            })
            .catch(() => {
              this.confirmLoading = false
              this.$message.error('修改失败,请联系管理员')
            })
        } else {
        }
      })
    },
    handleZh(info) {
      this.$router.push({ path: `/cashier/account/${info.recordAccId}` })
    },
    handleCancel() {
      this.visible = false
      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
  },
}
</script>
